package com.bbx.demo.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HeartbeatServerHandle extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt)  {
        String ms= "";
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;

            switch (idleStateEvent.state()){
                case ALL_IDLE:
                    ms = "全部空闲触发";
                    break;
                case READER_IDLE:
                    ms = "读空闲触发";
                    break;
                case WRITER_IDLE:
                    ms = "写空闲触发";
                    break;
            }

        }
        log.info("ms[{}]",ms);
        ctx.channel().close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("[{}]", cause);
        super.exceptionCaught(ctx, cause);
    }
}
